use clap::Parser;
use cliargs::CliArgs;
use log::{error, info};
use video_subtitle_gen_rs::{
    cliargs, helper,
    video_extrator::{self, SrtExtractResult},
};

/*
1.从视频中提取音频
2.使用whisper库将音频转换为字幕文本
3.逐条翻译字幕
4.将翻译后的字幕写入视频
 */
#[tokio::main(flavor = "current_thread")]
async fn main() {
    env_logger::init();
    let args = CliArgs::parse();
    match args.command {
        cliargs::Command::AudioConvert(audio_convert_args) => {
            let result = video_extrator::audio_convert(
                &audio_convert_args.video_file,
                &audio_convert_args.output_file_name,
            );
            match result {
                Ok(audio_path) => {
                    info!("success, audio file path: {}", audio_path.to_str().unwrap())
                }
                Err(err) => error!("{err}"),
            }
        }
        cliargs::Command::SrtExtract(srt_extract_args) => {
            let result = video_extrator::srt_extract(
                &srt_extract_args.audio_file,
                true,
                &srt_extract_args.srt_file,
            );
            match result {
                Ok(audio_path) => {
                    if let SrtExtractResult::WirteIo(srt_path) = audio_path {
                        info!("success, srt extracted path: {}", srt_path)
                    } else {
                        error!("error, srt extracted path is not found")
                    }
                }
                Err(err) => error!("{err}"),
            }
        }
        cliargs::Command::SrtTranslate(srt_translate_args) => {
            video_extrator::srt_translate(&srt_translate_args.srt_file, &None)
                .await
                .unwrap();
        }
        cliargs::Command::MergeVideo => todo!(),
        cliargs::Command::ConvertAll(args) => {
            let audio_path = video_extrator::audio_convert(&args.video_file, &None).unwrap();
            let srt_path = video_extrator::srt_extract(&audio_path, false, &None).unwrap();
            match srt_path {
                SrtExtractResult::Subtitle(mut subtitle) => {
                    video_extrator::srt_translate_impl(&mut subtitle, &None)
                        .await
                        .unwrap();
                    let video_file_name = helper::get_filename_exclude_extension(&args.video_file);
                    let srt_file_path = args
                        .video_file
                        .with_file_name(format!("{}srt", video_file_name));
                    subtitle.write_to_file(srt_file_path.to_str().unwrap());
                }
                _ => error!("error, srt extracted path is not found"),
            }
        }
    }
}
